package au.com.betaversion.service.fileload;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.betaversion.domain.Aircraft;
import au.com.bytecode.opencsv.CSVReader;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class FileLoaderCanada implements FileLoader {
	private static final Logger logger = LoggerFactory.getLogger(FileLoaderCanada.class);
	
	public void loadFileToCollectio(DBCollection collection) {
		File carscurr = null;
		File carsownr = null;
		CSVReader csvReader = null;
		
		Map<String, Aircraft> aircrafts = new HashMap<String, Aircraft>();
		
		try {
			carscurr = new File("/Users/mradochonski/Downloads/ccarcsdb/carscurr.txt");
			
			csvReader = new CSVReader(new FileReader(carscurr), ',');
			String [] nextLine;
		    while ((nextLine = csvReader.readNext()) != null) {
		    	Aircraft aircraft = new Aircraft();
		    	aircraft.setMark("C-" + nextLine[0].trim());
		    	aircraft.setManufacturer(nextLine[7].trim());
		    	aircraft.setSerialNumber(nextLine[5].trim());
		    	aircraft.setModel(nextLine[4].trim());
		    	aircrafts.put("C-" + nextLine[0].trim(), aircraft);
		    }
		    
		    carsownr = new File("/Users/mradochonski/Downloads/ccarcsdb/carsownr.txt");
		    csvReader = new CSVReader(new FileReader(carsownr), ',');
		    
		    while ((nextLine = csvReader.readNext()) != null) {
		    	String mark = "C-" + nextLine[0].trim();
		    	
		    	Aircraft aircraft = aircrafts.get(mark);
		    	
		    	if(aircraft != null) {
		    		aircraft.setOperatorName(nextLine[1].trim());
		    		aircraft.setOperatorState(nextLine[6].trim());
		    		DBObject object = new BasicDBObject();
		    		object.put("_id", mark);
		    		object.put("manufacturer", aircraft.getManufacturer());
		    		object.put("model", aircraft.getModel());
		    		object.put("serialNumber", aircraft.getSerialNumber());
		    		object.put("operatorName", aircraft.getOperatorName());
		    		object.put("operatorState", aircraft.getOperatorState());
		    		
		    		collection.save(object);
		    	}
		    }
		} catch (ArrayIndexOutOfBoundsException e) {
			logger.error(e.getMessage());
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			try {
				csvReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		FileLoader fileLoader = new FileLoaderCanada();
		MongoDbHelper dbHelper = new MongoDbHelper();
		
	    if(!dbHelper.authenticate()) {
	    	System.out.println("Authentication failed");
	    }

	    System.out.println("Authenticated...");
	    
		DBCollection collection = dbHelper.getCollection("aircraft");
		System.out.println("Working on 'aircraft' collection");
		
		fileLoader.loadFileToCollectio(collection);
	}
	
}
